var manifest = {
	settings:[{
		name: "resetme",
		type: "boolean",
		label: "Reset&nbsp;BlastOff?",
		default: false
	},
	{
		name: "showtips",
		type: "boolean",
		label: "Show&nbsp;Notifications?",
		default: true
	}]
};
jetpack.future.import("storage.settings");
jetpack.future.import("storage.simple");

var storage = jetpack.storage.simple;
storage.urls = (storage.urls && storage.urls.length > 0) ? storage.urls:[];
storage.tiptoggle = (storage.tiptoggle) ? storage.tiptoggle:false;
storage.tipselect = (storage.tipselect) ? storage.tipselect:false;
storage.active = (storage.active) ? storage.active:true;
storage.current = (storage.current) ? storage.current:false;

var pack = { 
	'title': 'BlastOff Notice:',
	'toggleon': 'BlastOff Enabled',
	'toggleoff': 'BlastOff Disabled',
	'tip': 'Mouse Over and Click the Section you want to BlastOff. Right Click to Cancel.',
	'success': 'We Have BlastOff! Protect with Blast Shield? Click Cancel to Undo These Changes.',
	'add': ' was added to BlastOff.',
	'exists': ' already exists with BlastOff.',
	'selecttip': 'TIP: The BlastOff icon activates on sites protected by the BlastShield. It will be dimmed on sites not currently under protection. Clicking this icon will allow you to add new protection to any section of any website. To disable selection mode, right click anywhere on the page. PS. You will only see this message once per installation.',
	'toggletip': 'TIP: The BlastShield icon toggles BlastOff Protection Mode. When it is active, protection is enabled. When it is dimmed, protection is disabled. PS. You will only see this message once per installation.',
	'images': {
		'blastactive':'%3D',
		'blastinactive':'%3D%3D',
		'shieldactive':'',
		'shieldinactive':'%3D'
	}
};

function notify(message){
	if(jetpack.storage.settings.showtips == true){
		jetpack.notifications.show({title:pack.title, body:message, icon:pack.images.blastactive});
	}
}
function toggleOff(widget){
	storage.active = false;
	for(var i in storage.urls){
		if(storage.urls[i].url == jetpack.tabs.focused.contentDocument.location.hostname){
			$(jetpack.tabs.focused.contentDocument).find(storage.urls[i].element).show();
		}
	}
	statusIcon(widget);
}
function toggleOn(widget){
	resetMe();
	storage.active = true;
	storage.current = false;
	for(var i in storage.urls){
		if(storage.urls[i].url == jetpack.tabs.focused.contentDocument.location.hostname){
			storage.current = true;
			$(jetpack.tabs.focused.contentDocument).find(storage.urls[i].element).hide();
		}
	}
	statusIcon(widget);
}
function statusIcon(widget){
	activesrc = (storage.active == true) ? pack.images.shieldactive:pack.images.shieldinactive;
	currentsrc = (storage.current == true) ? pack.images.blastactive:pack.images.blastinactive;
	$('#toggle',widget).attr('src',activesrc);
	$('#addnew',widget).attr('src',currentsrc);
}
function toggle(widget){
	resetMe();
	if(storage.tiptoggle==false){
		storage.tiptoggle = true;
		jetpack.tabs.focused.contentWindow.alert(pack.toggletip);
	}
	if(storage.active == true){
		storage.active = false;
		notify(pack.toggleoff);
		toggleOff(widget);
	}
	else {
		storage.active = true;
		notify(pack.toggleon);
		toggleOn(widget);
	}
	statusIcon(widget);
}
function selectElement(widget){
	notify(pack.tip);
	if(storage.tipselect==false){
		storage.tipselect = true;
		jetpack.tabs.focused.contentWindow.alert(pack.selecttip);
	}
	storage.current = true;
	$('*',jetpack.tabs.focused.contentDocument).mouseover(
		function(evt){
			evt.stopPropagation();
			$(jetpack.tabs.focused.contentDocument).bind("contextmenu",
				function(e){
					$('*',this).unbind('mouseover').unbind('mouseout').unbind('click');
					$('*',this).css('outline','');
				}
			);
			$(this,evt.target).css('outline','1px solid red').click(
				function(e){
					$('*',jetpack.tabs.focused.contentDocument).unbind('mouseover').unbind('mouseout').unbind('click');
					$('*',jetpack.tabs.focused.contentDocument).css('outline','');
					var id = ($(this,e.target).attr('id')!='') ? '#'+$(this,e.target).attr('id'):'.'+$(this,e.target).attr('class');
					if(id!=''){
						$(this,e.target).css('outline','').hide();
					}
					if(id!=''&&jetpack.tabs.focused.contentWindow.confirm(pack.success)){
						var url = { 
							'url':jetpack.tabs.focused.contentDocument.location.hostname,
							'element':id
						};
						storage.current = true;
						storage.active = true;
						addUrl(url);
					}
					else {
						$('*',jetpack.tabs.focused.contentDocument).unbind('mouseover').unbind('mouseout').unbind('click');
						$('*',jetpack.tabs.focused.contentDocument).css('outline','');
						if(id!=''){
							$(this,e.target).show();
						}
						storage.current = false;
					}
					e.stopPropagation();
				}
			);
		}
	);
	$('*',jetpack.tabs.focused.contentDocument).mouseout(
		function(evt){
			evt.stopPropagation();
			$('*',jetpack.tabs.focused.contentDocument).css('outline','');
		}
	);
	statusIcon(widget);
}
function addUrl(website){
	var exists = false;
	if(storage.urls.length>0){
		for(var i in storage.urls){
			if(storage.urls[i].url == website.url&&storage.urls[i].element == website.element){
				exists = true;
			}
		}
	}
	if(!exists){
		storage.urls.push(website);
		notify(website.url+pack.add);
	}
	else {
		notify(website.url+pack.exists);
	}
}
function resetMe(){
	if(jetpack.storage.settings.resetme == true){
		storage.urls = [];
		storage.tiptoggle = false;
		storage.tipselect = false;
		storage.active = false;
		storage.current = false;
	}
}
jetpack.statusBar.append({
    html: '<img src="'+pack.images.blastinactive+'" id="addnew" style="vertical-align: bottom; padding-top: 3px;" width="16" height="16" border="0" /><img src="'+pack.images.shieldinactive+'" id="toggle" style="vertical-align: bottom; margin-left: 3px;  padding-top: 3px;" width="16" height="16" border="0" />',  
    width: 35,  
    onReady: function(widget){
		function initialize(){
			if(storage.active){
				setTimeout(
					function(){
						toggleOn(widget);
					},
					500
				);
			}
		}
		$('#toggle',widget).click(
			function(e){
				toggle(widget);
			}
		);
		$('#addnew',widget).click(
			function(e){
				selectElement(widget);
			}
		);
		jetpack.tabs.onReady(initialize);
    }  
});